<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="理由名称" prop="reasonDetailName">
      <el-input v-model="dataForm.reasonDetailName" placeholder="理由名称"></el-input>
    </el-form-item>
    <el-form-item label="上级理由" prop="reasonId">
		<el-select v-model="dataForm.reasonId">
			<el-option
			 v-for="item in reasonList"
			 :label="item.reasonName"
			 :key="item.reasonId"
			 :value="item.reasonId">
			</el-option>
		</el-select>
    </el-form-item>
    <el-form-item label="理由内容">
      <el-input v-model="dataForm.reasonDetailContent" placeholder="理由内容"></el-input>
    </el-form-item>
    <el-form-item label="备注">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
		  reasonList:[],
        visible: false,
				disabledSubmit:false,
        dataForm: {
          reasonDetailId: 0,
          reasonDetailName: '',
          reasonId: '',
          reasonDetailContent: '',
          sellerId: '',
          remark: ''
        },
        dataRule: {
          reasonDetailName: [
            { required: true, message: '理由内容不能为空', trigger: 'blur' }
          ],
          reasonId: [
            { required: true, message: '理由ID不能为空', trigger: 'blur' }
          ],
          reasonDetailContent: [
            { required: true, message: '理由内容不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '租户不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
		  this.$http({
		    url: this.$http.adornUrl('/store/tstorereason/list'),
		    method: 'get',
		  }).then(({data}) => {
		    if (data && data.code === 0) {
		      this.reasonList = data.page.list
		    } else {
		      this.reasonList = []
		    }
		  })

        this.dataForm.reasonDetailId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.reasonDetailId) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorereasondetail/info/${this.dataForm.reasonDetailId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.reasonDetailName = data.tStoreReasonDetail.reasonDetailName
                this.dataForm.reasonId = data.tStoreReasonDetail.reasonId
                this.dataForm.reasonDetailContent = data.tStoreReasonDetail.reasonDetailContent
                this.dataForm.sellerId = data.tStoreReasonDetail.sellerId
                this.dataForm.remark = data.tStoreReasonDetail.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorereasondetail/${!this.dataForm.reasonDetailId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'reasonDetailId': this.dataForm.reasonDetailId || undefined,
                'reasonDetailName': this.dataForm.reasonDetailName,
                'reasonId': this.dataForm.reasonId,
                'reasonDetailContent': this.dataForm.reasonDetailContent,
                'sellerId': this.dataForm.sellerId,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
